package cn.shyouchi.wuguanjia.android.ui.attendance.statistics;

import android.content.Intent;
import android.widget.TextView;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.ui.statistic.UserMonthRank;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.SignCalendar;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class StaffStatisticsActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_position_name)
    TextView mTvPositionName;
    @BindView(R.id.calendar_view)
    SignCalendar mCalendarView;
    @BindView(R.id.tv_check_info)
    TextView mTvCheckInfo;

    AttendanceApi mAttendanceApi;

    SignCalendarCellDecorator mDecorator;

    UserStorage mUserStorage;

    UserMonthRank entity;
    String date;

    @Override
    public int initContentView() {
        return R.layout.activity_staff_statistics;
    }

    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        mUserStorage = UserStorageProvider.Default();
        entity = getIntent().getParcelableExtra("userid");
        date = getIntent().getStringExtra("date");


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mDecorator = new SignCalendarCellDecorator();
        mCalendarView.setSignCalendarCellDecorator(mDecorator);
        mCalendarView.initMonthView();
        mTvPositionName.setText(String.format("%1s | %2s", mUserStorage.getUserData().getName(), mUserStorage.getUserData().getPermissions_name()));

        if (entity != null && date != null) {
            getStatisticInfo(String.valueOf(entity.getId()), date);
            mTvPositionName.setText(String.format("%1s | %2s", entity.getName(), entity.getPermissions()));
        } else {
            getStatisticInfo();
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.tv_check_info)
    public void onClick() {
        Intent intent = new Intent(mContext, SignErrorInfoActivity.class);
        intent.putParcelableArrayListExtra("error", mDecorator.getSignErrorData());
        startActivity(intent);
    }


    private void getStatisticInfo() {
        mAttendanceApi.getStatistics()
                .subscribeOn(Schedulers.io())
                .map(new Func1<StatisticsInfoEntity, List<StatisticsInfoEntity.ResultMessageBean>>() {
                    @Override
                    public List<StatisticsInfoEntity.ResultMessageBean> call(StatisticsInfoEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<StatisticsInfoEntity.ResultMessageBean>>() {
                    @Override
                    public void onCompleted() {
                        mCalendarView.initMonthView();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtil.showToast("获取考勤信息失败");
                    }

                    @Override
                    public void onNext(List<StatisticsInfoEntity.ResultMessageBean> results) {
                        mDecorator.setData(results);
                    }
                });
    }


    private void getStatisticInfo(String userid, final String date) {


        mAttendanceApi.getStatistics(userid, date)
                .subscribeOn(Schedulers.io())
                .map(new Func1<StatisticsInfoEntity, List<StatisticsInfoEntity.ResultMessageBean>>() {
                    @Override
                    public List<StatisticsInfoEntity.ResultMessageBean> call(StatisticsInfoEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<StatisticsInfoEntity.ResultMessageBean>>() {
                    @Override
                    public void onCompleted() {
//                        mCalendarView.initMonthView();
                        try {
                            String[] ca = date.split("-");
                            Calendar calendar = Calendar.getInstance();
                            calendar.set(Calendar.YEAR, Integer.parseInt(ca[0]));
                            calendar.set(Calendar.MONTH, Integer.parseInt(ca[1]) - 1);
                            mCalendarView.initMonthView(calendar);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtil.showToast("获取考勤信息失败");
                    }

                    @Override
                    public void onNext(List<StatisticsInfoEntity.ResultMessageBean> results) {
                        mDecorator.setData(results);
                    }
                });
    }
}
